<?php

/**
 * Compras form base class.
 *
 * @package    tercermilenio
 * @subpackage form
 * @author     Your name here
 */
class BaseComprasForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'cpr_id'               => new sfWidgetFormInputHidden(),
      'cpr_nro_factura'      => new sfWidgetFormInput(),
      'cpr_proveedor'        => new sfWidgetFormPropelChoice(array('model' => 'Proveedores', 'add_empty' => false)),
      'cpr_fecha'            => new sfWidgetFormDateTime(),
      'cpr_usuario'          => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => false)),
      'cpr_total'            => new sfWidgetFormInput(),
      'detalle_compras_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Productos')),
    ));

    $this->setValidators(array(
      'cpr_id'               => new sfValidatorPropelChoice(array('model' => 'Compras', 'column' => 'cpr_id', 'required' => false)),
      'cpr_nro_factura'      => new sfValidatorString(array('max_length' => 20)),
      'cpr_proveedor'        => new sfValidatorPropelChoice(array('model' => 'Proveedores', 'column' => 'pvd_id')),
      'cpr_fecha'            => new sfValidatorDateTime(),
      'cpr_usuario'          => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id')),
      'cpr_total'            => new sfValidatorNumber(),
      'detalle_compras_list' => new sfValidatorPropelChoiceMany(array('model' => 'Productos', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('compras[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Compras';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalle_compras_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleComprass() as $obj)
      {
        $values[] = $obj->getDetCprArticulo();
      }

      $this->setDefault('detalle_compras_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetalleComprasList($con);
  }

  public function saveDetalleComprasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_compras_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleComprasPeer::DET_CPR_COMPRA, $this->object->getPrimaryKey());
    DetalleComprasPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_compras_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetalleCompras();
        $obj->setDetCprCompra($this->object->getPrimaryKey());
        $obj->setDetCprArticulo($value);
        $obj->save();
      }
    }
  }

}
